package com.only4play.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.only4play.captcha.CaptchaConfig;
import com.only4play.captcha.CaptchaService;
import com.only4play.captcha.impl.CaptchaServiceImpl;
import com.only4play.common.PasswordGenerator;
import com.only4play.idgenerator.provider.IdGenerator;
import com.only4play.idgenerator.provider.IdGeneratorOptions;
import com.only4play.idgenerator.provider.IdGeneratorProvider;
import com.only4play.idgenerator.strategy.IdGenerateStrategy;
import com.only4play.json.mapper.CustomObjectMapper;
import com.only4play.jwt.JwtCodec;
import com.only4play.jwt.JwtConfig;
import com.only4play.jwt.impl.JwtCodecImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author liyuncong
 * @version 1.0
 * @file UserCenterConfiguration
 * @brief 用户中心配置
 * @details 用户中心配置
 * @date 2023-11-18
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2023-11-18               liyuncong          Created
 */
@Configuration
public class UserCenterConfiguration {

    /**
     * 指定密码加密方式
     *
     * @return encoder
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        // 使用BCrypt加密
        return new BCryptPasswordEncoder();
    }

    /**
     * 密码生成器
     *
     * @return generator
     */
    @Bean
    public PasswordGenerator passwordGenerator() {
        return new PasswordGenerator() {
        };
    }

    /**
     * 唯一ID生成器实现类
     *
     * @return IdGeneratorProvider
     */
    @Bean
    public IdGeneratorProvider idGeneratorProvider() {
        IdGeneratorOptions options = new IdGeneratorOptions();
        // 设置生成策略为UUID
        options.setGenerateStrategy(IdGenerateStrategy.UUID);
        return new IdGeneratorProvider(options);
    }

    /**
     * 唯一ID生成器
     *
     * @return IdGenerator
     */
    @Bean("idGenerator")
    public IdGenerator idGenerator() {
        return idGeneratorProvider().instance();
    }

    /**
     * 验证码生成服务配置
     *
     * @return captchaConfig
     */
    @Bean
    public CaptchaConfig captchaConfig() {
        return new CaptchaConfiguration();
    }

    /**
     * 验证码生成服务
     *
     * @return CaptchaService
     */
    @Bean("captchaService")
    public CaptchaService captchaService() {
        return new CaptchaServiceImpl();
    }

    /**
     * objectMapper
     *
     * @return objectMapper
     */
    @Bean
    public ObjectMapper objectMapper() {
        return new CustomObjectMapper();
    }

    /**
     * token生成器
     *
     * @return JwtCodec
     */
    @Bean("jwtCodec")
    public JwtCodec<?> jwtCodec() {
        return new JwtCodecImpl<>(objectMapper());
    }

    /**
     * jwtConfig
     *
     * @return jwtConfig
     */
    @Bean("jwtConfig")
    public JwtConfig jwtConfig() {
        return new TokenConfiguration();
    }

}
